package com.foreknow.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.RegistLevel;
import com.foreknow.service.RegistLevelService;
import com.foreknow.utils.Constance;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api
@RestController
@RequestMapping(value = "RegistLevelController")
public class RegistLevelController {

    @Autowired
    private RegistLevelService registLevelService;

    @RequestMapping(value = "queryRegistLevelForAll")
    public String queryRegistLevelForAll() throws Exception {

        List<RegistLevel> list = registLevelService.queryRegistLevelForAll();

        HashMap<String, Object> hs = new HashMap<>();


        hs.put("data", list);
        hs.put("meta", new Constance("msg", 200));

        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);


    }

    @RequestMapping(value = "/updateRegistLevel")
    public String updateRegistLevel(@RequestBody Map<String, Object> para) throws Exception {
        RegistLevel registLevel = new RegistLevel();
        registLevel.setId((Integer) para.get("id"));
        registLevel.setRegistcode(para.get("registcode").toString());
        registLevel.setRegistname(para.get("registname").toString());
        registLevel.setRegistquota(Integer.parseInt((String) para.get("registquota")));
        registLevel.setRegistfee(new BigDecimal((String) para.get("registfee")));
        registLevel.setDelmark(1);


        HashMap map = new HashMap();

        if (registLevelService.updateRegistLevel(registLevel) == 1) {
            map.put("meta", new Constance("msg", 200));

        } else {
            map.put("meta", new Constance("msg", 500));

        }
        ObjectMapper objectMapper = new ObjectMapper();

        return objectMapper.writeValueAsString(map);
    }


    @RequestMapping(value = "/addRegistLevel")
    public String addRegistLevel(@RequestBody Map<String, Object> para) throws Exception {
        RegistLevel registLevel = new RegistLevel();
        registLevel.setRegistcode(para.get("registcode").toString());
        registLevel.setRegistname(para.get("registname").toString());
        registLevel.setRegistquota(Integer.parseInt((String) para.get("registquota")));
        registLevel.setRegistfee(new BigDecimal((String) para.get("registfee")));
        registLevel.setDelmark(1);

        HashMap map = new HashMap();

        if (registLevelService.addRegistLevel(registLevel) == 1) {
            map.put("meta", new Constance("msg", 200));

        } else {
            map.put("meta", new Constance("msg", 500));

        }
        ObjectMapper objectMapper = new ObjectMapper();

        return objectMapper.writeValueAsString(map);
    }

    @RequestMapping(value = "/deleteRegistLevel")
    public String deleteRegistLevel(@RequestBody Map<String, Object> para) throws Exception {

        Integer id = (Integer) para.get("id");

        HashMap map = new HashMap();

        if (registLevelService.deleteRegistLevel(id) == 1) {
            map.put("meta", new Constance("msg", 200));

        } else {
            map.put("meta", new Constance("msg", 500));

        }
        ObjectMapper objectMapper = new ObjectMapper();

        return objectMapper.writeValueAsString(map);
    }


}
